<template>
	<view class="container">
	   	<view style="">
			<!-- <u-dropdown :close-on-click-mask="true" ref="uDropdown" activeColor="#2979ff"
			 			:borderBottom="true" >
			 	<u-dropdown-item     v-model="shopSlect" 
			 	:title="title1 || '请选择店铺'"
			 	@change='dropdownChange' :options="shops">
			  
			 	</u-dropdown-item>
			 	 
			 </u-dropdown> -->
			 <u-row customStyle="background: #eee; height: 80rpx;padding-left:30rpx;">
			 	<u-col >
			 		<u--text prefixIcon='/static/icon/shopN.png' size='15' iconStyle="margin-right:10rpx" v-if="shop.shop_name " :text="'当前店铺：'+shop.shop_name  "></u--text>
			 		<u--text prefixIcon='/static/icon/shopN.png' size='15' iconStyle="margin-right:10rpx" v-else text="当前店铺： 未选择店铺 "></u--text>
			 	</u-col>
			 </u-row>
			 <view style="padding: 0 32rpx;">
			 	<u-empty
			 			iconSize='120'
			 			textSize='18'
			 			marginTop='90'
			 	        mode="data"
			 	       v-if="partner.length <=0"
			 	>
			 	</u-empty>
			 	
			 	<u-swipe-action v-else   >
			 		<u-swipe-action-item  
			 		
			 			:options="options1"
			 			 class="u-m-t-30"
			 			 :threshold = '30' 
			 			 @click='delet(item.id)'
			 			 v-for="(item,index) in partner"
			 		>
			 			 <u-row  customStyle="margin: 16rpx 0;" >
			 			 	<u-col span="3">
			 			 		<u--image  radius="4" :showLoading="true" :src="item.avatar" width="60px" height="60px" @click="click"></u--image>
			 			 	</u-col>
			 			 	
			 			 	<u-col span="9">
			 			 		<u-row>
			 			 			<u-col span="8">
			 			 				<u-row>
			 			 					<u-col span="7">
			 			
			 			 						<u--text :text='item.real_name' size='16'  ></u--text>
			 			 					</u-col>
			 			 					<u-col span="4">
			 			 						 
			 			 					</u-col>
			 			 				</u-row>
			 			 				<u-row>
			 			 					<u-col>
			 			 						<u--text :text="'联系方式:'+item.mobile" size='12' color='#878787'></u--text>
			 			 					</u-col>
			 			 				</u-row>
			 			 				<u-row>
			 			 					<u-col>
			 			 						<!-- <u--text text='录入总数:4' size='12' color='#878787'></u--text> -->
			 			 					</u-col>
			 			 					<u-col>
			 			 						<!-- <u--text text='今日总数:4' size='12' color='#878787'></u--text> -->
			 			 					</u-col>
			 			 				</u-row>
			 			 			</u-col>
			 			 			<u-col span="2" offset="1">
			 							<u-tag
			 							v-if="userInfo.id == item.id"
			 							style="white-space: nowrap;"
			 							text="店主"
			 							type="warning"
			 							size="mini"
			 								 
			 							></u-tag>
			 			 				<u-tag
			 							v-else
			 							style="white-space: nowrap;"
			 							text="已加入"
			 							type="warning"
			 							size="mini"
			 								 
			 			 				>
			 			 				</u-tag>
			 			 			</u-col>
			 			 		</u-row>
			 			 	</u-col>
			 			 </u-row>
			 			<u-line class="u-m-t-20"></u-line>
			 		</u-swipe-action-item>
			 	 
			 	</u-swipe-action>
				<u--text class="u-m-t-20" text='【提醒】' color='#7A7A7A' customStyle="margin: 32rpx 0;"></u--text>
				<u--text class="u-m-t-20" color='#7A7A7A' text='1.向左侧滑动,可移除店员。只有店主才有移除权限'></u--text>
				<u--text class="u-m-t-20" color='#7A7A7A' text='2.若扫码邀请店员不成功,请店员删除 商品过期管家 小程序后,再次打开微信扫码;'></u--text>
				<u--text class="u-m-t-20" color='#7A7A7A' text='3.若看不到店员昵称和头像,让店员在个人中心,点击获取昵称后,就能显示出来'></u--text>
				<u--text class="u-m-t-20" color='#7A7A7A' text='4.点击编辑昵称,修改个人在店铺中姓名,方便工作沟通'></u--text>
				<u-modal :show="modalShow"  title='提示' 
						cancelText='返回' 
						:closeOnClickOverlay='true'
						@cancel='cancel' 
						:showCancelButton='true' 
						confirmText='去解锁'  
						:content='mContent'
						@close='cancel'
						@confirm="confirm" ref="uModal" 
						:asyncClose="true"></u-modal>
						<view class="" style="position: fixed;bottom: 20rpx;width: 700rpx;">
							<u-row >
								<u-col span="4"  customStyle="margin-bottom: 32rpx;" >
									<u-button type="primary" text="短信邀请" @click="popupShow = true"></u-button>
								</u-col>
							 
								<u-col span="7" offset="1" customStyle="margin-bottom: 32rpx;">
									<u-button type="success" text="转发邀请" openType="share" @click="subAply"></u-button>
								</u-col>
							 
								
							</u-row>
						</view>
			 </view>
			
			
		</view>			
		
	
		<u-popup :show="popupShow" mode='bottom'  @open="open" :round="true"  borderRadius="12"  >
			<view class="" style="padding: 32rpx;">
				<u-row class="u-m-t-20 ">
					<u-col span="3" customStyle="margin: 0 auto;">
						<u--text text='邀请店员' size='18' color='#3F7FBF'></u--text>
					</u-col>
				</u-row>
				
				<view class="" class="u-m-t-20 ">
					<u--text color='#C14242' text='输入店员手机号码进行邀请,一旦店员使用商品过期管家时,会收到邀请通知!'></u--text>
				</view>
				<u-row   customStyle="margin-top: 32rpx;">
					<u-col span="3" offset="2"  >
						<u--text text='店员手机号'></u--text>
					</u-col>
					<u-col span="6" customStyle="margin: 0 auto;">
						<u--input
						    placeholder="请输入11位手机号"
						    border="surround"
							type='number'
							maxlength=11
						    v-model="number"
						    @change="changeNum"
						  ></u--input>
					</u-col>
				</u-row>
				<u-row customStyle="margin: 32rpx 0;"> 
					<u-col span="4" >
						<u-button   text="取消" @click="canAply"></u-button>
					</u-col>
					<u-col span="4"  ></u-col>
					<u-col span="4"  >
						<u-button text="提交邀请" type="primary"  @click="subAplyByNum"></u-button>
					</u-col>
				</u-row>
			</view>
			
		</u-popup>
	</view>
</template>

<script> 
	var that
	var ERGoodsScan = require('../../../service/ERGoodsScan'),mERGoodsScan
	var ERShoopManage = require('../../../service/ERShoopManage.js'),mERShoopManage
	var ERShareGift = require('../../../service/ERShareGift.js'),mERShareGift
	var UserInfo = require('../../../service/UserInfo.js'),mUserInfo
	export default {
	 onShareAppMessage(res) {
	 		return {
	 			title: '邀请加入店铺',
	 			path: '/pages/app/home/home?type=104&userId=' + that.userInfo.id + '&userName=' + that.userInfo.real_name+'&shopId='+that.shopId
	 		}
	     },
		data() {
			return {
				level:[{
					type:'一店一人',
					addStaf:false,
					addShop:false,
				},{
					type:'一店多人',
					addStaf:true,
					addShop:false,
				},{
					type:'多店多人',
					addStaf:true,
					addShop:true,
				}],
				 options1: [{
						text: '移除'
					}],
				shopData:[],
				number:'' ,
				popupShow:false,
				mContent:'邀请店员仅对会员开发，升级会员后即可解锁此功能！',
				modalShow: false,
				focus:false,
				edit:'编辑昵称',
				src: 'https://cdn.uviewui.com/uview/album/1.jpg',
				name:'全栈开发云',
				inputDisable:false,
				userInfo:{},
				users:[],
				shopId:'',
				partner:[],
				shops:[],
				shopSlect:1,
				title1:'',
				shop:{}
				
			}
		},
		onLoad(options) {
			that = this
			that.shopId =options.shopId
			mERShoopManage = new ERShoopManage(that.mNetTool)
			mUserInfo = new UserInfo(that.mNetTool)
			mERShareGift = new ERShareGift(that.mNetTool)
			mERGoodsScan = new ERGoodsScan(that.mNetTool)
		},
		onShow() {
			let userInfo = uni.getStorageSync('userInfo')
			let isLogin = uni.getStorageSync('isLogin')
			let viewShopListItem = uni.getStorageSync('viewShopListItem')
			if(viewShopListItem){
				that.shop = viewShopListItem
				that.shopId = viewShopListItem.id
			}else{
				that.getShops()
			}
			if(!isLogin){
				uni.showModal({
					content:'请登录后使用',
					success(r) {
						if(r.confirm){
							if (that.autoStatus) { //支持一键登录
								console.log('进入一键登录')
								that.keyLogin()
							} else {
								uni.navigateTo({
									url: '../login/login'
								})
							}
						}else {
							uni.navigateBack({
								
							})
							return
						}
					}
				})
			}
			// if(userInfo.level ==0 || userInfo.level ==1 || userInfo.level ==2 ){
			// 	that.modalShow = false
			// }else{
			// 	that.modalShow = true
			// }
		},
		mounted() {
			let userInfo = uni.getStorageSync('userInfo')
			that.userInfo = userInfo
			// if(!userInfo){
			// 	uni.showModal({
			// 		content:'请登录后使用',
			// 		success(r) {
			// 			if(r.confirm){
			// 				if (that.autoStatus) { //支持一键登录
			// 					console.log('进入一键登录')
			// 					that.keyLogin()
			// 				} else {
			// 					uni.navigateTo({
			// 						url: '../login/login'
			// 					})
			// 				}
			// 			}else {
			// 				uni.navigateBack({
								
			// 				})
			// 			}
			// 		}
			// 	})
			// }
			if(false){
				let ss = uni.getStorageSync('shopList')
				
				let aa = []
				console.log('ss',ss)
				if(ss) {
					ss.forEach(item => {
						if(item.shop_name){
							var stmp = {
								label:item.shop_name,
								value:item.id
							}
							aa.push(stmp)
						}
						
					})
					console.log('aa',aa)
					that.shops = aa
					that.title1 = aa[0].label
					that.shopId = aa[0].value
				 
				} 
			}
			that.getStaffs()
		},
		 
		methods: {
			getShops(){
				let ss = uni.getStorageSync('shopList')
				if(ss){
					that.shop = ss[0]
				}else{
					mERShoopManage.list({
						own_user_id: userInfo.id
					},res => {
						that.shop = res.rows[0]
						uni.setStorageSync('shopList',res.rows)
					})
				}
			},
			dropdownChange(e){
				console.log(e)
				 // uni.showLoading({
				 // 	title:'请稍等'
				 // })
				  let shopName
				that.shops.forEach( item => {
					if(item.value == e){
						that.title1 = item.label
						that.shopId = item.value
						console.log(item.label)
						shopName ={
							id: item.value,
							shop_name:item.label
						}
					}
				})
				uni.setStorageSync('viewShopListItem',shopName)
			 	that.getStaffs()
			},
			getStaffs(){
				uni.showLoading({
					title:'查询中'
				}) 
				console.log(that.shopId)
				mERShoopManage.getStafList({
					store_id:that.shopId
				},res => {
					if(res){
						that.partner = res.rows
						uni.hideLoading()
					}
				})
			 
				
			},
			subAplyByNum(){
				let userInfo = uni.getStorageSync('userInfo')
				if(!that.shopId){
					uni.$u.toast('请选择一个店铺')
					console.log(that.shopId)
					return
				}
				if(userInfo.level == 0 || userInfo.level == -2){
					that.popupShow = false
					uni.showModal({
						content:'您当前的套餐不支持多人，请升级',
						confirmText:'去升级',
						success(e) {
							if(e.confirm){
								uni.navigateTo({
									url:'../VIP/VIP'
								})
							}
						}
					})
					return
				}
				//短信邀请
				let info = {
					promoter_id:userInfo.id,
					invitation_phone:that.number,
					inviter_shop_id:userInfo.store_id,	
					inviter_name:userInfo.real_name,	
					status:0
				}
				if(that.shopId){
					info.inviter_shop_id = that.shopId
				}
				if(that.number.length != 11){
					uni.$u.toast('请输入正确的手机号码')
					return
				}
				// if(that.$util.checkStr(that.number,'mobile')) {
				// 	console.log('校验通过')
				// }else{
				// 	console.log('校验shiab')
				// }
				mERShareGift.list({
					invitation_phone:that.number,
					status:0
				},res =>{
					console.log(res)
					if(res.records != 0){
						uni.$u.toast('已邀请过，无需重复邀请')
						return
					} else{
					 console.log('--------------',info)
					 debugger
						mERShareGift.save(info,res=>{
							console.log(res)
							if(res.success){
								uni.$u.toast('发送邀请成功！')
								that.popupShow = false
							}else{
								uni.$u.toast('未知错误')
							}
						}) 
					}
				}) 
				
			},
			delet(i){
				let that = this
				let viewShopListItem = uni.getStorageSync('viewShopListItem')
				let userInfo = uni.getStorageSync('userInfo')
				if(i == userInfo.id){
					uni.$u.toast('不可移除自己')
					return
				}
				if(viewShopListItem){
					if(viewShopListItem.owner_id){
						if(viewShopListItem.owner_id  != userInfo.id){
							uni.$u.toast('您不是店主，无权限操作！')
							return
						}
						
					}else{
						let shopList = uni.getStorageSync('shopList')
						if(shopList){
							shopList.forEach(item => {
								if(item.id == viewShopListItem.id){
									viewShopListItem = item
									uni.setStorageSync('viewShopListItem',viewShopListItem)
									if(viewShopListItem.owner_id  != userInfo.id){
										uni.$u.toast('您不是店主，无权限操作！')
										return
									}
									
								}
							})
						}else{
							uni.$u.toast('获取店铺信息有误，正在更新信息...')
							mERShoopManage.list({
								own_user_id: userInfo.id
							},res => {
								shopList = res.rows
								shopList.forEach(item => {
									if(item.id == viewShopListItem.id){
										viewShopListItem = item
										uni.setStorageSync('viewShopListItem')
										if(viewShopListItem.owner_id  != userInfo.id){
											uni.$u.toast('您不是店主，无权限操作！')
											return
										}
										
									}
								})
							})
							return
						}
					}
					console.log(viewShopListItem.owner_id  , userInfo.id)
					
				}
				uni.showModal({
					content:'确定删除吗?',
					success(res) {
						if(res.confirm){
							if(that.shopId){
								// 如果shopid存在就通过shopid查找
								ERShoopManage.del({
									user_id:that.userInfo.id,
									store_id:that.shopId
								},res => {
									
								})
							}else{
								 
							}
						}
					}
				}) 
			},
			subAply(){
				 
				 
			},
			canAply(){
				that.popupShow = false
			},
			changeNum(){
				console.log(that.number)
			},
			confirm(){
				
			},
			cancel(){
				that.modalShow = false
			},
			editName(){
				uni.navigateTo({
					url:'editName'
				})
				// console.log(123)
				// if(this.inputDisable == true ){
				// 		.inputDisable = false
				// 	this.focus = true
				// 	this.edit = '确认'
				// }else {
				// 	this.inputDisable = true
				// 	this.focus = false
				// 	this.edit = '编辑昵称'
				// }
			}
		}
	}
</script>

<style lang="scss">
	.container {
		 
	}
 
	 
</style>
